import { defineStore } from 'pinia'

const login = (): Promise<string> => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('userid')
    }, 2000);
  })
}
// defineStore('user', ...) 和下面的形式相同
export const useUserStore = defineStore({
  id: 'user',
  state: () => ({
    userid: '',
    counter: 0
  }),
  getters: {
    doubleCount: (state) => state.counter * 2,
    doublePlusOne(): number {
      // 等同于调用其他getter: return this.doubleCount + 1
      return this.counter * 2 + 1
    },
    payloadCount() {
      return (payload: number) => this.doublePlusOne + payload
    }
  },
  actions: {
    setUserId(payload: string) {
      this.userid = payload
    },
    async getUser() {
      console.log('asd')
      // this.setUserId(await login())
      this.userid = await login()
    }
  }
})
